package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.adapter.personCenter.ClearWeedExchangeAdapter;
import com.zhongxi.guoyuan.application.MyApplication;
import com.zhongxi.guoyuan.entity.HomeSearchResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.CommonTool;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by qj on 2018/4/21.
 */

public class TopSearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //普通布局
    static final int TYPE_ITEM = 0;
    //底部布局
    static final int TYPE_FOOTER = 1;

    private int footer_state=0;
    //正在加载更多
    static final int LOAD_MORE = 1;

    //没有更多
    static final int NO_MORE = 2;

    //初始状态
    static final int INIT = 0;

    private OnItemClickListener itemClickListener;

    private Context context;
    private List<HomeSearchResult.DataBean> mList;
    private JSONObject cateInfo;
    private JSONObject store;

    private HomeSearchResult.DataBean mDataBean;


    public TopSearchAdapter(Context context) {
        mList=new ArrayList<>();
        this.context = context;
    }

    public void setList(List<HomeSearchResult.DataBean> list,JSONObject cateInfo,JSONObject store) {
        mList = list;
        this.cateInfo=cateInfo;
        this.store=store;
        notifyDataSetChanged();
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setFooter_state(int footer_state) {
        this.footer_state = footer_state;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = View.inflate(context, R.layout.top_search_layout, null);
            MyViewHolder mMyViewHolder=new MyViewHolder(view);
            return mMyViewHolder;

        } else if (viewType == TYPE_FOOTER) {
            View view = View.inflate(context, R.layout.recycle_footer_layout, null);
            FooterViewHolder mFooterViewHolder=new FooterViewHolder(view);
            return mFooterViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        if (holder instanceof MyViewHolder){

            mDataBean = mList.get(position);
            if (mDataBean != null) {
                if (mDataBean.getType().equals("offlinemarketstore")){
                    ((MyViewHolder) holder).mRlSearchLeft.setVisibility(View.GONE);
                    ((MyViewHolder) holder).mRlSearch2Left.setVisibility(View.VISIBLE);
                    ((MyViewHolder) holder).mRlSearch3Left.setVisibility(View.GONE);
                    if(null!=mDataBean.getName()&&!"".equals(mDataBean.getName())){
                        ((MyViewHolder) holder).mTvSearch2Name.setText(mDataBean.getName());
                    }else{
                        ((MyViewHolder) holder).mTvSearch2Name.setText("");
                    }
                    Glide.with(context)
                            .load(Url.IMG_URL+mDataBean.getThumbnail())
                            .asBitmap()
                            .placeholder(R.mipmap.hicc)
                            .centerCrop()
                            .into(((MyViewHolder) holder).mIvSearch2Image);
                    if(!"".equals(mDataBean.getOrdernum())){
                        ((MyViewHolder) holder).mTvSearch2Hasnum.setText(mDataBean.getOrdernum()+"人已消费");
                    }else{
                        ((MyViewHolder) holder).mTvSearch2Hasnum.setText("0人已消费");
                    }
                    if(null!=mDataBean.getAddress()&&!"".equals(mDataBean.getAddress())){
                        ((MyViewHolder) holder).mTvSearch2Address.setText(mDataBean.getAddress());
                    }else{
                        ((MyViewHolder) holder).mTvSearch2Address.setText("");
                    }
                    if(null!=mDataBean.getSubject()&&!"".equals(mDataBean.getSubject())){
                        ((MyViewHolder) holder).mTvSearch2Type.setText(mDataBean.getSubject());
                    }else{
                        ((MyViewHolder) holder).mTvSearch2Type.setText("");
                    }

                    ((MyViewHolder) holder).mTvSearch2Distance.setText("距您："+ CommonTool.getDistance(Double.parseDouble(mDataBean.getLongitude()),
                            Double.parseDouble(mDataBean.getLatitude()),
                            Double.parseDouble(MyApplication.getInstance().getLongitude()),
                            Double.parseDouble(MyApplication.getInstance().getLatitude())));


                    if (!mDataBean.getDiscount().equals("100")){
                        ((MyViewHolder) holder).mTvSearch2Welcome.setText(Float.parseFloat(mDataBean.getDiscount())/10.0f+"折结账");
                    }else {
                        ((MyViewHolder) holder).mTvSearch2Welcome.setText("欢迎光临");
                    }


                }else if (mDataBean.getType().equals("exchangegoods")){
                    ((MyViewHolder) holder).mRlSearchLeft.setVisibility(View.VISIBLE);
                    ((MyViewHolder) holder).mRlSearch2Left.setVisibility(View.GONE);
                    ((MyViewHolder) holder).mRlSearch3Left.setVisibility(View.GONE);

                    if(null!=mDataBean.getName()&&!"".equals(mDataBean.getName())){
                        ((MyViewHolder) holder).mTvSearchName.setText(mDataBean.getName());
                    }else{
                        ((MyViewHolder) holder).mTvSearchName.setText("");
                    }
                    Glide.with(context)
                            .load(Url.IMG_URL+mDataBean.getThumbnail())
                            .asBitmap()
                            .placeholder(R.mipmap.hicc)
                            .centerCrop()
                            .into(((MyViewHolder) holder).mIvSearchImage);

                    if(!"".equals(mDataBean.getOrdernum())){
                        ((MyViewHolder) holder).mTvSearchHasnum.setText(mDataBean.getOrdernum()+"人已兑");
                    }else{
                        ((MyViewHolder) holder).mTvSearchHasnum.setText("0人已兑");
                    }

                    if(null!=store&&!"".equals(store)){
                        JSONObject mObject = store.optJSONObject(mDataBean.getSid());
                        if(null!=mObject&&!"".equals(mObject)){
                            ((MyViewHolder) holder).mTvSearchShopname.setText("商家:"+ mObject.optString("name"));
                            String mLongitude = mObject.optString("longitude");
                            String mLatitude = mObject.optString("latitude");

                            if (mObject.optString("amountofmail").equals("0")){
                                ((MyViewHolder) holder).mTvPost.setVisibility(View.VISIBLE);
                            }else {
                                ((MyViewHolder) holder).mTvPost.setVisibility(View.GONE);
                            }

                            ((MyViewHolder) holder).mTvSearchDistance.setText("距您："+ CommonTool.getDistance(Double.parseDouble(mLongitude),
                                    Double.parseDouble(mLatitude),
                                    Double.parseDouble(MyApplication.getInstance().getLongitude()),
                                    Double.parseDouble(MyApplication.getInstance().getLatitude())));
                        }
                    }
                    if(null!=mDataBean.getMarketprice()&&!"".equals(mDataBean.getMarketprice())){
                        ((MyViewHolder) holder).mTvSearchOriginprice.setText("原价："+mDataBean.getMarketprice());
                        ((MyViewHolder) holder).mTvSearchOriginprice.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    }else{
                        ((MyViewHolder) holder).mTvSearchOriginprice.setText("");
                    }


                    if(null!=mDataBean.getInnerprice()&&!"".equals(mDataBean.getInnerprice())){
                        ((MyViewHolder) holder).mTvSearchPrice.setText(mDataBean.getInnerprice()+"美豆");
                    }else{
                        ((MyViewHolder) holder).mTvSearchPrice.setText("");
                    }
                    if(null!=mDataBean.getRedbeanprice()&&!"".equals(mDataBean.getRedbeanprice())){
                        ((MyViewHolder) holder).mTvSearchRedPrice.setText(" + "+mDataBean.getRedbeanprice()+"红豆");
                    }else{
                        ((MyViewHolder) holder).mTvSearchRedPrice.setText("");
                    }
                    

                }else if (mDataBean.getType().equals("marketgoods")){
                    ((MyViewHolder) holder).mRlSearchLeft.setVisibility(View.GONE);
                    ((MyViewHolder) holder).mRlSearch2Left.setVisibility(View.GONE);
                    ((MyViewHolder) holder).mRlSearch3Left.setVisibility(View.VISIBLE);

                    if(null!=mDataBean.getName()&&!"".equals(mDataBean.getName())){
                        ((MyViewHolder) holder).mTvSearch3Name.setText(mDataBean.getName());
                    }else{
                        ((MyViewHolder) holder).mTvSearch3Name.setText("");
                    }
                    Glide.with(context)
                            .load(Url.IMG_URL+mDataBean.getThumbnail())
                            .asBitmap()
                            .placeholder(R.mipmap.hicc)
                            .centerCrop()
                            .into(((MyViewHolder) holder).mIvSearch3Image);

                    if(!"".equals(mDataBean.getOrdernum())){
                        ((MyViewHolder) holder).mTvSearch3Hasnum.setText("销量"+mDataBean.getOrdernum());
                    }else{
                        ((MyViewHolder) holder).mTvSearch3Hasnum.setText("销量0");
                    }

                    if(!"".equals(mDataBean.getFloorprice())){
                        ((MyViewHolder) holder).mTvSearch3Price.setText(mDataBean.getFloorprice()+"元");
                        ((MyViewHolder) holder).mTvSearch3Send.setText("奖励"+mDataBean.getFloorprice()+"红豆");
                    }else{
                        ((MyViewHolder) holder).mTvSearch3Price.setText("");
                        ((MyViewHolder) holder).mTvSearch3Send.setText("");
                    }
                }

            }

        }else if (holder instanceof FooterViewHolder){
            switch (footer_state){
                case LOAD_MORE://
                    ((TopSearchAdapter.FooterViewHolder) holder).recycleProgressbar.setVisibility(View.VISIBLE);
                    ((TopSearchAdapter.FooterViewHolder) holder).recycleTvLine1.setVisibility(View.GONE);
                    ((TopSearchAdapter.FooterViewHolder) holder).recycleTvLine2.setVisibility(View.GONE);
                    ((TopSearchAdapter.FooterViewHolder) holder).recycleFootTv.setVisibility(View.VISIBLE);

                    ((TopSearchAdapter.FooterViewHolder) holder).recycleFootTv.setText("正在加载...");
                    break;
                case NO_MORE://
                    ((TopSearchAdapter.FooterViewHolder) holder).recycleProgressbar.setVisibility(View.GONE);
                    ((TopSearchAdapter.FooterViewHolder) holder).recycleTvLine1.setVisibility(View.GONE);
                    ((TopSearchAdapter.FooterViewHolder) holder).recycleTvLine2.setVisibility(View.GONE);
                    ((TopSearchAdapter.FooterViewHolder) holder).recycleFootTv.setVisibility(View.VISIBLE);

                    ((TopSearchAdapter.FooterViewHolder) holder).recycleFootTv.setText("已经到最底部了");
                    break;
                case INIT://上拉加载
                    ((TopSearchAdapter.FooterViewHolder) holder).recycleProgressbar.setVisibility(View.GONE);
                    ((TopSearchAdapter.FooterViewHolder) holder).recycleFootTv.setVisibility(View.GONE);
                    ((TopSearchAdapter.FooterViewHolder) holder).recycleTvLine1.setVisibility(View.GONE);
                    ((TopSearchAdapter.FooterViewHolder) holder).recycleTvLine2.setVisibility(View.GONE);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mList.size()>0?mList.size():0;
    }



    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @Bind(R.id.iv_search_image)
        ImageView mIvSearchImage;
        @Bind(R.id.tv_search_name)
        TextView mTvSearchName;
        @Bind(R.id.tv_search_price)
        TextView mTvSearchPrice;
        @Bind(R.id.tv_search_redprice)
        TextView mTvSearchRedPrice;
        @Bind(R.id.tv_search_originprice)
        TextView mTvSearchOriginprice;
        @Bind(R.id.tv_search_shopname)
        TextView mTvSearchShopname;
        @Bind(R.id.tv_search_post)
        TextView mTvPost;
        @Bind(R.id.tv_search_distance)
        TextView mTvSearchDistance;
        @Bind(R.id.tv_search_hasnum)
        TextView mTvSearchHasnum;
        @Bind(R.id.rl_search_left)
        RelativeLayout mRlSearchLeft;
        @Bind(R.id.iv_search2_image)
        ImageView mIvSearch2Image;
        @Bind(R.id.tv_search2_welcome)
        TextView mTvSearch2Welcome;
        @Bind(R.id.rl_2_left)
        RelativeLayout mRl2Left;
        @Bind(R.id.tv_search2_name)
        TextView mTvSearch2Name;
        @Bind(R.id.tv_search2_address)
        TextView mTvSearch2Address;
        @Bind(R.id.tv_search2_distance)
        TextView mTvSearch2Distance;
        @Bind(R.id.tv_search2_type)
        TextView mTvSearch2Type;
        @Bind(R.id.tv_search2_hasnum)
        TextView mTvSearch2Hasnum;
        @Bind(R.id.rl_search2_left)
        RelativeLayout mRlSearch2Left;
        @Bind(R.id.iv_search3_image)
        ImageView mIvSearch3Image;
        @Bind(R.id.tv_search3_name)
        TextView mTvSearch3Name;
        @Bind(R.id.tv_search3_send)
        TextView mTvSearch3Send;
        @Bind(R.id.tv_search3_price)
        TextView mTvSearch3Price;
        @Bind(R.id.tv_search3_hasnum)
        TextView mTvSearch3Hasnum;
        @Bind(R.id.rl_search3_left)
        RelativeLayout mRlSearch3Left;

        MyViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onClick(v,getAdapterPosition());

            }
        }
    }

    class FooterViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.recycle_tv_line1)
        TextView recycleTvLine1;
        @Bind(R.id.recycle_progressbar)
        ProgressBar recycleProgressbar;
        @Bind(R.id.recycle_foot_tv)
        TextView recycleFootTv;
        @Bind(R.id.recycle_tv_line2)
        TextView recycleTvLine2;
        @Bind(R.id.recycle_container_relative)
        RelativeLayout recycleContainerRelative;

        public FooterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public static interface OnItemClickListener{
        void onClick(View view, int position);
    }
}
